import { defineStore } from 'pinia'
import Api from '@/apis/user'
import myaxios from '@/utils/request'
type LoginForm = {
  username: string
  password: string
}
const useUserStore = defineStore('user', {
  state: () => ({
    userinfo: null,
    token: null
  }),
  actions: {
    // 登录
    async userlogin(params: LoginForm) {
      const api = Api.getUser
      const res = await myaxios(api.url, api.method, params)
      if (res.data.code == 200) {
        this.token = res.data.data.token
        return res
      } else {
        return res
      }
    },
    // 获取用户信息
    async getinfo() {
      const api = Api.getinfo
      const res = await myaxios(api.url, api.method)
      if (res.data.code == 200) {
        this.userinfo = res.data.data?.checkUser
        return res.data.data
      }
    },
    logout() {
      this.token = null
      this.userinfo = null
    }
  },
  persist: true
})

export default useUserStore
